package com.sz.mes.controller;

import com.sz.mes.dto.EquipmentDTO;
import com.sz.mes.entity.Equipment;
import com.sz.mes.entity.Technique;
import com.sz.mes.mapper.TechniqueMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.sz.mes.entity.Tech;
import com.sz.mes.dto.TechDTO;
import com.sz.mes.vo.TechListVO;
import com.sz.mes.vo.TechDetailVO;
import com.sz.mes.service.TechService;
import com.sz.mes.controller.BaseCustomCrudRestController;
import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;
import java.util.List;

/**
 * 工艺信息查询 相关Controller
 *
 * @author hem
 * @version 1.0
 * @date 2023-08-01
 * Copyright © szhs
 */
@RestController
@RequestMapping("/tech")
@BindPermission(name = "工艺信息查询")
@Slf4j
public class TechController extends BaseCustomCrudRestController<Tech> {

    @Autowired
    private TechService techService;

    @Autowired
    private TechniqueMapper techniqueMapper;

    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    // @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(TechDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, TechListVO.class);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    // @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
        return super.getViewObject(id, TechDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    // @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody Tech entity) throws Exception {
        if (!StringUtils.isEmpty(entity.getTechName())) {
            // Technique technique = new Technique();
            // BeanUtils.copyProperties(entity, technique);
            // techniqueMapper.insert(technique);
            return super.createEntity(entity);
        } else {
            return JsonResult.FAIL_OPERATION("工艺名称不能为空");
        }
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    // @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody Tech entity) throws Exception {
        return super.updateEntity(id, entity);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    // @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
        return super.deleteEntity(id);
    }

    /**
     * 查询预计工序详情
     *
     * @param taskNum
     * @return
     */
    @Log(operation = OperationCons.LABEL_LIST)
    // @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/getList")
    public List<Tech> getList(@RequestParam String taskNum) {
        return techService.getList(taskNum);
    }
    /**
     * 根据设备信息查询任务单
     *
     * @param
     * @return
     */
    @Log(operation = OperationCons.LABEL_LIST)
    // @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @PostMapping("/getTaskInfo")
    public JsonResult getTaskInfo(@RequestBody Equipment equipment) {
        return techService.getTaskInfo(equipment);
    }


}
